package com.pti.usermanager.service.impl;

import com.pti.usermanager.dao.api.OrgDao;
import com.pti.usermanager.enums.ConstantCode;
import com.pti.usermanager.service.api.OrgService;
import com.pti.usermanager.web.dto.request.SaveOrgRequest;
import com.pti.usermanager.web.dto.request.UpdateOrgRequest;
import com.pti.usermanager.web.dto.response.ResultResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: OrgServiceImpl
 * @Author: mahuilin
 * @Description: 组织业务实现层
 * @Date: 2019/2/20
 */
@Service
public class OrgServiceImpl implements OrgService {

    private static final Logger logger = LogManager.getLogger(OrgServiceImpl.class);
    @Autowired
    private OrgDao orgDao;
    @Override
    public ResultResponse findTreeByOrg(Long usrId,Long compId){
        logger.debug("usrId:{}",usrId);
        logger.debug("compId:{}",compId);

        Map<String, Object> inputMap = new HashMap<>();
        String roleCd = orgDao.findRoleByUsrId(usrId);
        inputMap.put("usrId",usrId);
        inputMap.put("roleCd",roleCd);
        inputMap.put("compId",compId);

        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(orgDao.findOrgTreeByUsrRole(inputMap));
        return resultResponse;
    }

    @Override
    public ResultResponse saveOrg(SaveOrgRequest saveOrgRequest) {
        logger.debug("saveOrgRequest:{}",saveOrgRequest.toString());

        ResultResponse resultResponse = new ResultResponse();
        try {
            orgDao.saveOrg(saveOrgRequest);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }
        return resultResponse;
    }

    @Override
    public ResultResponse delOrg(Long orgId) {
        logger.debug("orgId:{}",orgId.toString());

        ResultResponse resultResponse = new ResultResponse();
        try {
            orgDao.delOrg(orgId);
        }catch (Exception e){
            logger.error(e.getMessage(), e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }

        return resultResponse;
    }

    @Override
    public ResultResponse updateOrg(UpdateOrgRequest updateOrgRequest) {
        logger.debug("updateOrgRequest:{}",updateOrgRequest.toString());
        ResultResponse resultResponse =new ResultResponse();
        try {
            orgDao.updateOrg(updateOrgRequest);
        }catch (Exception e){
            logger.error(e.getMessage(), e);
            resultResponse.setCode(ConstantCode.DTO.SYS_ERROR_CODE);
            resultResponse.setMessage(ConstantCode.DTO.SYS_ERROR_MSG);
        }
        return resultResponse;
    }
}
